//获取标签对象
const oIpt = document.querySelector('input');
const oUl = document.querySelector('ul');

//定义一个变量存储延时器
let tt = 0;

//获取输入框焦点获取/失去
oIpt.addEventListener('focus',function(){
    oUl.style.display = 'block';
})

oIpt.addEventListener('blur',function(){
    oUl.style.display = 'none';
})


oIpt.addEventListener('input',function(){
    clearInterval(tt);

    tt = setTimeout(function(){
        setPage();
    },1000);
})

function setPage(){
    let keyword = oIpt.value;

    const data = new Date();
    let time = data.getTime();

    const s = document.createElement('script');

    s.setAttribute('src',`https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=35834,35106,31253,35488,34584,35490,35872,35801,35797,35317,26350,35885,35746&wd=${keyword}&req=2&csor=3&pwd=11&cb=fun&_=${time}`);

    document.body.appendChild(s);

    document.body.removeChild(s);
}

function fun(response){
    if(response.g === undefined){
        oUl.style.display = 'none';
    }else{
        oUl.style.display = 'block';

        let str = '';

        response.g.forEach(function(item){
            str += `<li>${item.q}</li>`
        })
        oUl.innerHTML = str;
    }
}